package com.example.okhttp3;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class MainActivity extends AppCompatActivity {
    private static final String TAG ="MainActivity" ;
    private static final int GET_CONTENT =1001 ;
    private static final int POST_PHONE = 1002;
    private static final int GET_PHONE = 1003;
    private Button btnAysnGet;
    private TextView txtResult;
    private Handler handler;
    private Button btnGet;
    private Button btnPostPhone;
    private OkHttpClient okHttpClient;
    private Button btnGetPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnAysnGet = findViewById(R.id.btn_asynGet);
        txtResult  = findViewById(R.id.txt_result);
        btnGet     = findViewById(R.id.btn_get);
        btnPostPhone = findViewById(R.id.btn_postPhone);
        okHttpClient = new OkHttpClient();
        btnGetPhone  = findViewById(R.id.btn_getPhone);
        handler= new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if(msg.what==GET_CONTENT){
                    String content = (String) msg.obj;
                    txtResult.setText(content);
                    return true;
                }else if(msg.what==POST_PHONE){
                    String content = (String) msg.obj;
                    txtResult.setText(content);
                    return true;
                }else if(msg.what ==GET_PHONE) {
                    String content = (String) msg.obj;
                    txtResult.setText(content);
                    return true;

                }
                return false;
            }
        });

        /**
         * GET方式查询手机号归属地
         */
        btnGetPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url ="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=13415181111&userID= ";

                final Request request = new Request.Builder()
                        .url(url)
                        .get()
                        .build();
                final Call call = okHttpClient.newCall(request);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Response response =call.execute();

                            String result = response.body().string();
                            Message message = Message.obtain();
                            message.what = GET_PHONE;
                            message.obj  = result;
                            handler.sendMessage(message);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();
            }

        });

        /***
         * POST方式查询手机号归属地
         */

        btnPostPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RequestBody requestBody = new FormBody.Builder()
                        .add("mobileCode","1345648651")
                        .add("userID","")
                        .build();
                Request request = new Request.Builder()
                        .url("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo")
                        .post(requestBody)
                        .build();
                okHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG, "onFailure: "+e);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Message message = Message.obtain();
                        message.what = POST_PHONE;
                        message.obj  = result;
                        handler.sendMessage(message);
                    }
                });
            }
        });

        /**
         * 同步Get
         */
        btnGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url ="https://www.baidu.com";

                final Request request = new Request.Builder()
                        .url(url)
                        .get()
                        .build();
                final Call call = okHttpClient.newCall(request);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Response response =call.execute();

                            String result = response.body().string();
                            Message message = Message.obtain();
                            message.what = GET_CONTENT;
                            message.obj  = result;
                            handler.sendMessage(message);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();
            }
        });

        /**
         * 异步Get
         */
        btnAysnGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url ="https://www.baidu.com";

                final Request request = new Request.Builder()
                        .url(url)
                        .get()
                        .build();
                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG, "onFailure: "+e);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
//                        Log.e(TAG, "onResponse: "+response.body().string());
                        String result = response.body().string();
                        Message message = Message.obtain();
                        message.what = GET_CONTENT;
                        message.obj  = result;
                        handler.sendMessage(message);
                    }
                });
            }
        });
    }
}
